﻿using AutoMapper;
using BS.Infra;
using BS.Infra.Interface;
using MediatR;

namespace BS.Write.Api.Handler.Breeding
{
    public class DelBreedingCommandHandler : IRequestHandler<DelBreedingCommand, ApiResult>
    {
        private readonly IBreedingsRepository _breedingsRepository;

        private readonly ILogger<DelBreedingCommandHandler> _logger;

        private readonly IMapper _mapper;

        public DelBreedingCommandHandler(IBreedingsRepository breedingsRepository,
            ILogger<DelBreedingCommandHandler> logger,
            IMapper mapper)
        {
            _breedingsRepository = breedingsRepository;
            _logger = logger;
            _mapper = mapper;
        }
        public async Task<ApiResult> Handle(DelBreedingCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var breeding = await _breedingsRepository.GetAsync(x=>x.BreedingId== request.BreedingId);
                if (breeding.BreedingId == null)
                {
                    return  ApiResult.Fail("未找到养殖信息");
                }
                breeding.IsDeleted = true;
                await _breedingsRepository.UpdateAsync(breeding);
                return ApiResult.Ok(breeding);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error deleting breeding");
                return  ApiResult.Fail("Error deleting breeding");
            }
        }
    }
}
public class DelBreedingCommand:IRequest<ApiResult>
{
    public int BreedingId { get; set; }
}